import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.junit.Test;  

public class testCheckEachOutputIsCorrect {           
	private String fileName = "NewEvent.ics"; //CHANGE TO MATCH FILE TO TEST
	private String commaTest = "Test, if commas, are replaced."; //DO NOT CHANGE
	
	/*
	 * Test to ensure creation of .ics is correct according to input 
	 * by user using both valid and invalid entries. Checks all lines of file.
	 */
	@Test
	public void testFileIntegrity() throws IOException {
		 FileReader readFile = new FileReader(fileName); 
	     BufferedReader in = new BufferedReader(readFile); 
	     assertEquals(in.readLine(),"BEGIN:VCALENDAR"); //Should always pass test
	     assertEquals(in.readLine(),"VERSION:2.0");  //Should always pass test
	     assertTrue(in.readLine().startsWith("CALSCALE:")); //Should always pass test
	     assertTrue(in.readLine().startsWith("METHOD:PUBLISH")); //Should always pass test
	     assertTrue(in.readLine().matches("X-WR-TIMEZONE:Hawaiian Standard Time|Alaskan Standard Time|Pacific Standard Time|Mountain Standard Time|Central Standard Time|Eastern Standard Time"));
	     assertEquals(in.readLine(),"BEGIN:VTIMEZONE"); //Should always pass test
	     assertTrue(in.readLine().matches("TZID:Hawaiian Standard Time|Alaskan Standard Time|Pacific Standard Time|Mountain Standard Time|Central Standard Time|Eastern Standard Time"));
	     assertEquals(in.readLine(),"END:VTIMEZONE"); //Should always pass test
	     assertEquals(in.readLine(),"BEGIN:VEVENT"); //Should always pass test
	     assertNotNull(in.readLine()); //Date start
	     assertNotNull(in.readLine()); //Date end
	     assertNotNull(in.readLine()); //Date stamp
	     assertEquals(in.readLine(),"CLASS:PUBLIC"); //Should always pass test
	     assertTrue(in.readLine().startsWith("CREATED:")); //Should always pass test
	     assertTrue(in.readLine().length() < 50); //Max char amt at 50 for description
	     assertTrue(in.readLine().length() < 50); //Max char amt at 50 for comments
	     assertTrue(in.readLine().length() < 50); //Max char amt at 50 for resources
	     assertTrue(in.readLine().startsWith("LAST-MODIFIED:")); //Should always pass test
	     assertTrue(in.readLine().length() < 50); //Max char amt at 50 for location
	     assertTrue(in.readLine().matches("PRIORITY:0|1|9"));
	     assertTrue(in.readLine().startsWith("SEQUENCE:")); //Should always pass test
	     assertEquals(in.readLine(), "STATUS:CONFIRMED"); //Should always pass test
	     assertFalse(in.readLine().isEmpty()); //Make sure event name is not empty
	     assertTrue(in.readLine().startsWith("TRANSP:"));
	     assertEquals(in.readLine(),"END:VEVENT");
	     assertEquals(in.readLine(),"END:VCALENDAR");
	         
		} 
	
	
	/*
	 * Test to ensure creation of .ics is correct for all unchanging
	 * values in a .ics file, ensuring that the issue is with
	 * the user's input and not the file creation itself.
	 */
	@Test
	public void testAlwaysPass() throws IOException {
		 FileReader readFile = new FileReader("NewEvent2.ics"); 
	     BufferedReader in = new BufferedReader(readFile);
	     assertEquals(in.readLine(),"BEGIN:VCALENDAR"); 
	     assertEquals(in.readLine(),"VERSION:2.0"); 
	     assertTrue(in.readLine().startsWith("CALSCALE:GREGORIAN")); 
	     assertTrue(in.readLine().startsWith("METHOD:PUBLISH")); 
	     in.readLine();
	     assertEquals(in.readLine(),"BEGIN:VTIMEZONE");
	     in.readLine();
	     assertEquals(in.readLine(),"END:VTIMEZONE");
	     assertEquals(in.readLine(),"BEGIN:VEVENT");
	     assertNotNull(in.readLine());
	     assertNotNull(in.readLine());
	     assertNotNull(in.readLine());
	     assertEquals(in.readLine(),"CLASS:PUBLIC");
	     assertTrue(in.readLine().startsWith("CREATED:"));
	     in.readLine();
	     in.readLine();
	     in.readLine();
	     assertTrue(in.readLine().startsWith("LAST-MODIFIED:"));
	     in.readLine();
	     in.readLine();
	     assertTrue(in.readLine().startsWith("SEQUENCE:"));
	     assertEquals(in.readLine(), "STATUS:CONFIRMED");
	     assertFalse(in.readLine().isEmpty());
	     assertTrue(in.readLine().startsWith("TRANSP:"));
	     assertEquals(in.readLine(),"END:VEVENT");
	     assertEquals(in.readLine(),"END:VCALENDAR");
	     
	}  
     
	
	/*
	 * Test for the ReplaceCommas() method in the ICSFileCreator. Ensures
	 * that all commas in a string are replaced by //, for RFC accordance.
	 */
	@Test     
	public void testReplaceCommas() {	
		commaTest = icsFileCreator.replaceCommas(commaTest);
        assertEquals(commaTest,"Test\\, if commas\\, are replaced.");
	}
	
}
